package net.georel.representation.categorisation.datasource;


import java.awt.Color;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;



import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFactorySpi;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.DefaultQuery;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureSource;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Query;
import org.geotools.data.Transaction;
import org.geotools.data.crs.ReprojectFeatureResults;
import org.geotools.data.postgis.PostgisDataStoreFactory;
import org.geotools.data.store.ReprojectingFeatureCollection;
import org.geotools.data.wfs.WFSDataStoreFactory;
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.factory.Hints;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureCollections;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.SchemaException;
import org.geotools.feature.simple.SimpleFeatureTypeBuilder;
import org.geotools.geometry.jts.JTS;
import org.geotools.map.MapContext;
import org.geotools.ows.ServiceException;
import org.geotools.referencing.CRS;
import org.geotools.referencing.crs.DefaultGeographicCRS;
import org.geotools.styling.FeatureTypeStyle;
import org.geotools.styling.Fill;
import org.geotools.styling.Graphic;
import org.geotools.styling.Mark;
import org.geotools.styling.Rule;
import org.geotools.styling.Stroke;
import org.geotools.styling.Style;
import org.geotools.styling.StyleFactory;
import org.geotools.styling.Symbolizer;
import org.opengis.feature.Feature;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.GeometryDescriptor;
import org.opengis.filter.FilterFactory;
import org.opengis.filter.FilterFactory2;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.MultiLineString;
import com.vividsolutions.jts.geom.MultiPolygon;
import com.vividsolutions.jts.geom.Polygon;

public class WebFeatureService {
	private static DataStore wfs;
    private static FilterFactory filterFactory;
    private static FeatureSource<SimpleFeatureType, SimpleFeature> source;
      
    
	
	
public static FeatureCollection<SimpleFeatureType,SimpleFeature> getWFSdata(String tablename) throws ServiceException, IOException, FactoryException, SchemaException{
	
	//instead of adding the data to the map this just returns the featuresource so it can be used to analyse the data before visulising it.
	
	System.out.println("Fetching WFS Data....");
		//build connection URL and parameters, timeout parameter is important otherwise large data volumes will cause problems
		String getCapabilities = "http://schroeder.geo.uzh.ch:8080/geoserver/wfs?REQUEST=GetCapabilities&VERSION=1.0.0";
		
		Map connectionParameters = new HashMap();
		
		connectionParameters.put(WFSDataStoreFactory.URL.key, getCapabilities );
		connectionParameters.put(WFSDataStoreFactory.TIMEOUT.key, 300000 );
		
		
		// Step 2 - connection
		DataStore data = DataStoreFinder.getDataStore( connectionParameters );
		
		String typeNames[] = data.getTypeNames();
		
		for(int i = 0;i<typeNames.length;i++){
			//System.out.println(i+typeNames[i]);
			if(tablename.equals(typeNames[i])){
			String typeName = typeNames[i];
			SimpleFeatureType schema = data.getSchema(typeName);
			source = data.getFeatureSource(typeName);			
			}
		}
						
		FeatureCollection<SimpleFeatureType,SimpleFeature> featureCollection = source.getFeatures();
		
		data.dispose();
		return featureCollection;
		
		
	}
	
//builds the default style objects
	
public static FeatureCollection<SimpleFeatureType,SimpleFeature> getPostGISdata(String dbtype,
		String host,
		String port,
		String schema,
		String database,
		String user,
		String password,
		String tablename) throws IOException{
	//dbtype = "postgis", host = schroder.geo.uzh.ch, port = 5432
	FeatureSource<SimpleFeatureType,SimpleFeature> featureSource;
	
	Map config = new HashMap();
	config.put( PostgisDataStoreFactory.DBTYPE.key, dbtype );
	
	config.put( PostgisDataStoreFactory.HOST.key, host );
	config.put( PostgisDataStoreFactory.PORT.key, port );
	
	config.put( PostgisDataStoreFactory.SCHEMA.key, schema );
	config.put( PostgisDataStoreFactory.DATABASE.key, database );
	config.put( PostgisDataStoreFactory.USER.key, user );
	config.put( PostgisDataStoreFactory.PASSWD.key, password);		
	
	DataStore pgDataStore = DataStoreFinder.getDataStore(config);
	String[] typeNames = pgDataStore.getTypeNames();
	
	for(int i = 0;i<typeNames.length;i++){
		System.out.println(i+typeNames[i]);
		if(tablename.equals(typeNames[i])){
			String typeName = typeNames[i];
			
			// Step 3 - discovery
			SimpleFeatureType tableSchema = pgDataStore.getSchema(typeName);
			
						// Step 4 - target
			source = pgDataStore.getFeatureSource(typeName);
			
		}
	}
	FeatureCollection<SimpleFeatureType,SimpleFeature> featureCollection = source.getFeatures();
	return featureCollection;
	
}
	
	
	
}
